package com.cc

import android.os.Handler
import android.os.Message
import android.util.Log
import android.util.SparseArray
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import java.lang.ref.WeakReference
import java.util.*
import kotlin.random.Random

class DanmuEngine(var viewGroup: ViewGroup) {

    val viewCache = SparseArray<LinkedList<View>>()

    var handler: InnerHandler
    var defaultDanmuAnimator = DefaultDanmuAnimator()
    var danmuAnimator: DanmuAnimator = defaultDanmuAnimator
    val dammuQueue = LinkedList<DanmuMessage>()

    init {
        handler = InnerHandler(this)
        viewCache.put(viewCache.hashCode(), LinkedList())
    }

    fun post(danmuMessage: DanmuMessage) {
        val msg = handler.obtainMessage();
        msg.obj = danmuMessage
        handler.sendMessage(msg)
    }

    fun postDelay(danmuMessage: DanmuMessage, ms: Long) {
        val msg = handler.obtainMessage();
        msg.obj = danmuMessage
        handler.sendMessageDelayed(msg, ms)
    }

    private fun show(danmuMessage: DanmuMessage) {
        var isDefType = true
        val queue = if (danmuMessage.contentView == null) {
            isDefType = true
            danmuMessage.type = viewCache.hashCode()
            viewCache.get(danmuMessage.type)
        } else {
            isDefType = false
            val q = LinkedList<View>()
            viewCache.put(danmuMessage.type, q)
            q
        }
        if (queue.isEmpty()) {
            Log.d("Engine", "queue is empty,addView")
            if (!isDefType) {
                queue.add(danmuMessage.contentView!!)
            } else {
                val textView = TextView(viewGroup.context)
                queue.add(textView)
            }
        } else {
            Log.d("Engine", "queue is not empty,addView from cache")
        }
        viewCache.put(danmuMessage.type, queue)
        var target = queue.poll()
        if (isDefType) {
            val textView = target as TextView
            if (danmuMessage.textSize > 0) {
                textView.setTextSize(danmuMessage.textSize)
            }
            if (danmuMessage.textColor > 0) {
                textView.setTextColor(danmuMessage.textColor)
            }
            textView.text = danmuMessage.content
            target = textView
            danmuMessage.contentView = textView
        }
        viewGroup.addView(target)
        dammuQueue.add(danmuMessage)
        target?.translationY = Random.nextInt(400).toFloat()
        danmuAnimator.update(this, danmuMessage, viewGroup)
    }

    fun recycle(danmuMessage: DanmuMessage) {
        var queue = viewCache.get(danmuMessage.type)
        if (queue != null) {
            viewGroup.removeView(danmuMessage.contentView)
            queue.add(danmuMessage.contentView!!)
            viewCache.put(danmuMessage.type, queue)
        }
    }

    class InnerHandler(danmuEngine: DanmuEngine) : Handler() {
        var weakReference: WeakReference<DanmuEngine> = WeakReference(danmuEngine)
        override fun handleMessage(msg: Message) {
            val danmuEngine = weakReference.get()
            if (danmuEngine != null) {
                val danmuMessage: DanmuMessage = msg.obj as DanmuMessage
                danmuEngine.show(danmuMessage)
            }
        }
    }
}